/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.repository.mapper.evaluation;

import cn.ac.nercis.pes.model.dto.evaluation.asset.*;
import cn.ac.nercis.pes.repository.model.evaluation.AssetAttributeDO;
import cn.ac.nercis.pes.repository.model.evaluation.AssetDO;
import cn.ac.nercis.pes.repository.model.knowledge.KmAssetAttributeDO;
import lombok.NonNull;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;

import java.util.Optional;

/**
 * 测评过程相关信息类型映射
 *
 * @author zpy
 * @version 1.0.0
 */
@Mapper(componentModel = "spring",
        imports = {StringUtils.class,
                Optional.class},
        unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface AssetMapper {
    AssetMapper INSTANCE = Mappers.getMapper(AssetMapper.class);

    AssetDO toDO(PhyEnvDTO dto);
    AssetDO toDO(PhySecurityDTO dto);
    AssetDO toDO(PwdProductDTO dto);
    AssetDO toDO(ServerStorageDTO dto);
    AssetDO toDO(NetworkSecurityDTO dto);
    AssetDO toDO(DatabaseDTO dto);
    AssetDO toDO(KeyBusinessDTO dto);
    AssetDO toDO(SubBusinessDTO dto);
    AssetDO toDO(KeyDataDTO dto);
    AssetDO toDO(ChannelDTO dto);
    AssetDO toDO(ManageDocumentDTO dto);

    @Mapping(target = "id",ignore = true)
    @Mapping(target = "attributeId",source = "id")
    AssetAttributeDO toAssetAttributeDO(KmAssetAttributeDO entity);

    default PhyEnvDTO toPhyEnvDTO(@NonNull AssetDO assetDO){
        PhyEnvDTO phyEnvDTO = PhyEnvDTO.builder()
                .id(assetDO.getId())
                .isUse(assetDO.getIsUse())
                .desc(assetDO.getDesc())
                .quoteAssetId(assetDO.getQuoteAssetId())
                .build();
        if(CollectionUtils.isNotEmpty(assetDO.getAttributes())){
            assetDO.getAttributes().forEach(att->phyEnvDTO.setField(att.getFieldName(),att.getValue()));
        }
        return phyEnvDTO;
    }

    default PhySecurityDTO toPhySecurity(@NonNull AssetDO assetDO){
        PhySecurityDTO phySecurity = PhySecurityDTO.builder()
                .id(assetDO.getId())
                .isUse(assetDO.getIsUse())
                .desc(assetDO.getDesc())
                .quoteAssetId(assetDO.getQuoteAssetId())
                .build();
        if(CollectionUtils.isNotEmpty(assetDO.getAttributes())){
            assetDO.getAttributes().forEach(att->phySecurity.setField(att.getFieldName(),att.getValue()));
        }
        return phySecurity;
    }

    default PwdProductDTO toPwdProduct(@NonNull AssetDO assetDO){
        PwdProductDTO pwdProduct = PwdProductDTO.builder()
                .id(assetDO.getId())
                .isUse(assetDO.getIsUse())
                .desc(assetDO.getDesc())
                .quoteAssetId(assetDO.getQuoteAssetId())
                .build();
        if(CollectionUtils.isNotEmpty(assetDO.getAttributes())){
            assetDO.getAttributes().forEach(att->pwdProduct.setField(att.getFieldName(),att.getValue()));
        }
        return pwdProduct;
    }

    default ServerStorageDTO toServerStorage(@NonNull AssetDO assetDO){
        ServerStorageDTO serverStorage = ServerStorageDTO.builder()
                .id(assetDO.getId())
                .isUse(assetDO.getIsUse())
                .desc(assetDO.getDesc())
                .quoteAssetId(assetDO.getQuoteAssetId())
                .build();
        if(CollectionUtils.isNotEmpty(assetDO.getAttributes())){
            assetDO.getAttributes().forEach(att->serverStorage.setField(att.getFieldName(),att.getValue()));
        }
        return serverStorage;
    }

    default NetworkSecurityDTO toNetworkSecurity(@NonNull AssetDO assetDO){
        NetworkSecurityDTO networkSecurity = NetworkSecurityDTO.builder()
                .id(assetDO.getId())
                .isUse(assetDO.getIsUse())
                .desc(assetDO.getDesc())
                .quoteAssetId(assetDO.getQuoteAssetId())
                .build();
        if(CollectionUtils.isNotEmpty(assetDO.getAttributes())){
            assetDO.getAttributes().forEach(att->networkSecurity.setField(att.getFieldName(),att.getValue()));
        }
        return networkSecurity;
    }

    default DatabaseDTO toDatabase(@NonNull AssetDO assetDO){
        DatabaseDTO database = DatabaseDTO.builder()
                .id(assetDO.getId())
                .isUse(assetDO.getIsUse())
                .desc(assetDO.getDesc())
                .quoteAssetId(assetDO.getQuoteAssetId())
                .build();
        if(CollectionUtils.isNotEmpty(assetDO.getAttributes())){
            assetDO.getAttributes().forEach(att->database.setField(att.getFieldName(),att.getValue()));
        }
        return database;
    }

    default KeyBusinessDTO toKeyBusiness(@NonNull AssetDO assetDO){
        KeyBusinessDTO keyBusiness = KeyBusinessDTO.builder()
                .id(assetDO.getId())
                .isUse(assetDO.getIsUse())
                .desc(assetDO.getDesc())
                .quoteAssetId(assetDO.getQuoteAssetId())
                .build();
        if(CollectionUtils.isNotEmpty(assetDO.getAttributes())){
            assetDO.getAttributes().forEach(att->keyBusiness.setField(att.getFieldName(),att.getValue()));
        }
        return keyBusiness;
    }

    default SubBusinessDTO toSubBusiness(@NonNull AssetDO assetDO){
        SubBusinessDTO subBusiness = SubBusinessDTO.builder()
                .id(assetDO.getId())
                .isUse(assetDO.getIsUse())
                .desc(assetDO.getDesc())
                .quoteAssetId(assetDO.getQuoteAssetId())
                .build();
        if(CollectionUtils.isNotEmpty(assetDO.getAttributes())){
            assetDO.getAttributes().forEach(att->subBusiness.setField(att.getFieldName(),att.getValue()));
        }
        return subBusiness;
    }

    default KeyDataDTO toKeyData(@NonNull AssetDO assetDO){
        KeyDataDTO keyData = KeyDataDTO.builder()
                .id(assetDO.getId())
                .isUse(assetDO.getIsUse())
                .desc(assetDO.getDesc())
                .quoteAssetId(assetDO.getQuoteAssetId())
                .keyDataAssetId(assetDO.getKeyDataAssetId())
                .build();
        if(CollectionUtils.isNotEmpty(assetDO.getAttributes())){
            assetDO.getAttributes().forEach(att->keyData.setField(att.getFieldName(),att.getValue()));
        }
        return keyData;
    }

    default ChannelDTO toChannel(@NonNull AssetDO assetDO){
        ChannelDTO channel = ChannelDTO.builder()
                .id(assetDO.getId())
                .isUse(assetDO.getIsUse())
                .desc(assetDO.getDesc())
                .quoteAssetId(assetDO.getQuoteAssetId())
                .build();
        if(CollectionUtils.isNotEmpty(assetDO.getAttributes())){
            assetDO.getAttributes().forEach(att->channel.setField(att.getFieldName(),att.getValue()));
        }
        return channel;
    }

    default ManageDocumentDTO toManageDocument(@NonNull AssetDO assetDO){
        ManageDocumentDTO manageDocument = ManageDocumentDTO.builder()
                .id(assetDO.getId())
                .isUse(assetDO.getIsUse())
                .desc(assetDO.getDesc())
                .quoteAssetId(assetDO.getQuoteAssetId())
                .build();
        if(CollectionUtils.isNotEmpty(assetDO.getAttributes())){
            assetDO.getAttributes().forEach(att->manageDocument.setField(att.getFieldName(),att.getValue()));
        }
        return manageDocument;
    }
}